<?php

namespace app\admin\controller;

use think\facade\View;

class Login extends AdminBase
{
    public function initialize()
    {
        if ($this->isLogin()) {
            return $this->redirect(url("index/index"));
        }
    }

    public function index()
    {
        return View::fetch();
    }

    public function md5()
    {
//        halt(session(config("admin.session_admin")));
        $pwd = 123456;
        echo md5("admin_xu_".$pwd);
    }

    public function check()
    {

        $isPost = $this->request->isPost();
        if (!$isPost) {
            return show(config("status.error"), "请求方式不正确");
        }
        $username = input("param.username", '', 'trim');
        $password = input("param.password", '', 'trim');
        $captcha = input("param.captcha", '', 'trim');//        dump($password);exit();
        $data = [
            'username' => $username,
            'password' => $password,
            'captcha' => $captcha
        ];
        $validate = new \app\admin\validate\AdminUser();
        if (!$validate->check($data)) {
            return show(config("status.error"), $validate->getError());
        }
//            if (empty($username) || empty($password) || empty($captcha)) {
//                return show(config("status.error"), "参数不能为空");
//            }
//            if (!captcha_check($captcha)) {
//                return show(config("status.error"), "验证码不正确");
//            }
        try {
            $result = \app\admin\business\AdminUser::login($data);

        } catch (\Exception $exception) {
            return show(config("status.error"), $exception->getMessage());
        }
        if ($result) {
            return show(config("status.success"), "登陆成功");
        } else {
            return show(config("status.error"), "登陆失败");
        }

    }
}